#include <iostream>
#include "serialport/serialport.h"
#include "StringUtil.hpp"
#include "File.hpp"
#include "Thread.hpp"
using namespace std;

int main()
{
    string serialName = "/dev/pts/4";

    if (!sh::SerialPort::exists(serialName))
    {
        cout << "serialport is not exist" << endl;
        return 1;
    }
    cout << "serialport is exist" << endl;

    sh::SerialPort serialPort(serialName);
    serialPort.setReadTimeout(0);
    serialPort.setReadMinSize(0);
    if (!serialPort.open())
    {
        cout << "open serial port fail" << endl;
        return 1;
    }
    cout << "open serial port pass" << endl;
//    serialPort.setReadBlock(true);

    while (true)
    {
        char buff[4] = {0x00};
        int size = serialPort.read(buff, 4);
        if (size > 0)
        {
            cout << sh::StringUtil::format("read size: %d, content: %s", size, buff) << endl;
            if (sh::StringUtil::compare(buff, "quit") == 0)
            {
                cout << "read quit" << endl;
                break;
            }
        }
        else
        {
             cout << size << endl;
        }
        sh::Thread::msleep(100);
    }

    serialPort.close();

    return 0;
}
